import 'package:flutter_module/common/base/page/BasePage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../common/base/page/BasicPage.dart';
import '../../common/channel/ChannelConstant.dart';
import '../../common/router/RouterPath.dart';
import '../../res/colors.dart';
import '../../res/image.dart';
import 'account_setting_logic.dart';

/// @description:
/// @author
/// @date: 2023-12-07 09:19:46
class AccountSettingPage extends BasePage {
  final AccountSettingLogic logic = Get.put(AccountSettingLogic());
  final String phoneNumber;

  AccountSettingPage({super.key, required this.phoneNumber});

  @override
  State<StatefulWidget> createState() => _AccountSettingPageState();
}

class _AccountSettingPageState
    extends BasicPage<AccountSettingPage, AccountSettingLogic> {
  @override
  AccountSettingLogic get currentLogic => widget.logic;
  @override
  String screenName() {
    return "账号设置";
  }

  @override
  bool extendBodyBehindAppBar() {
    return true;
  }

  @override
  Widget body(BuildContext context) {
    // 处理微信昵称请求回调
    currentLogic.onDataReceived = (nickName) {
      if (nickName == "未绑定") {
        _showSuccessDialog();
      }
      // 刷新页面
      setState(() {});
    };
    return ListView.separated(
      itemBuilder: (c, i) {
        if (i == 0) {
          return Container(
            color: Colors.white,
            child: ListTile(
              tileColor: Colors.white,
              contentPadding: const EdgeInsets.symmetric(horizontal: 20.0),
              leading: const Text(
                "手机号",
                style: TextStyle(color: YouthColors.color_453938, fontSize: 15),
              ),
              trailing: Text(
                widget.phoneNumber,
                style: const TextStyle(
                    color: YouthColors.color_A29B9B, fontSize: 15),
              ),
            ),
          );
        }
        // 第二行
        else if (i == 1) {
          return Container(
            color: Colors.white,
            child: ListTile(
              tileColor: Colors.white,
              onTap: _clickUnBindWeChat,
              contentPadding: const EdgeInsets.only(left: 20, right: 15),
              leading: Row(
                children: <Widget>[
                  const Expanded(
                    flex: 1, // 根据需要调整flex值，以控制title和右侧内容的空间比例
                    child: Text(
                      "微信绑定",
                      style: TextStyle(
                          color: YouthColors.color_453938, fontSize: 15),
                    ),
                  ),
                  Expanded(
                    flex: 2, // 给右侧内容（subTitle和Image）更多的空间
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        const Spacer(), // 在subTitle和Image之间创建一些空间（可选）
                        Text(
                          currentLogic.weChatNickName,
                          style: const TextStyle(
                              color: YouthColors.color_17191D, fontSize: 15),
                          textAlign: TextAlign.right,
                        ),
                        currentLogic.weChatNickName != "未绑定"
                            ? Image.asset(
                                YouthImages.icRightArrow,
                                width: 32,
                                height: 32,
                              )
                            : const SizedBox.shrink(),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        } else {
          return Container(
            color: Colors.white,
            child: ListTile(
              tileColor: Colors.white,
              onTap: _accountLogout,
              contentPadding: const EdgeInsets.only(left: 20, right: 15),
              trailing: Image.asset(
                YouthImages.icRightArrow,
                width: 32,
                height: 32,
              ),
              title: const Text(
                "账号注销",
                style: TextStyle(color: YouthColors.color_453938, fontSize: 15),
              ),
            ),
          );
        }
      },
      separatorBuilder: (c, i) {
        if (i == 0) {
          return Container(
            height: 10,
            color: YouthColors.color_F8F7F7,
          );
        } else {
          return Container();
        }
      },
      itemCount: 3,
    );
  }

// --------------------------------
// MARK: Method
// --------------------------------

  _accountLogout() {
    //直接用BoostNavigator的跳转原生，会调用原生的回调方法跳转
    BoostNavigator.instance.push(ROUTER_CANCELLATION_ACCOUNT,
        arguments: {KEY_ROUTER_PARAMETERS: "?state=deleteWarn"},
        withContainer: true);
  }

  /// 点击解绑微信
  void _clickUnBindWeChat() {
    if (currentLogic.weChatNickName != "未绑定") {
      // 弹窗
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text("提示"),
            content: const Text("要解除与微信帐号的绑定吗？"),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop(); // 取消按钮
                },
                child: const Text("取消"),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop(); // 关闭对话框
                  // 请求解除绑定
                  currentLogic.requestWechatUnbinding();
                },
                child: const Text("确定"),
              ),
            ],
          );
        },
      );
    }
  }

  /// 显示成功弹窗，并在2秒后自动消失
  void _showSuccessDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        Future.delayed(const Duration(seconds: 2), () {
          Navigator.pop(context); // 2秒后关闭Dialog
        });

        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          elevation: 24.0,
          backgroundColor: Colors.white,
          child: Container(
            width: 285,
            height: 190,
            padding: const EdgeInsets.all(16.0), // 添加一些内边距
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SvgPicture.asset(
                  YouthImages.icon_Dialog_Success,
                  width: 48,
                  height: 48,
                ),
                const SizedBox(
                  height: 24,
                ), // 间距
                const Text(
                  '解绑成功',
                  style: TextStyle(
                    fontSize: 16,
                    color: YouthColors
                        .color_17191D, // 确保这里使用了正确的颜色代码，或者定义了一个Color常量
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
